package com.example.exception;


import com.example.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

/**
 *
 * 全局异常捕获
 */

@Slf4j
@RestControllerAdvice
public class GlobaIExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result ex(Exception ex){
        ex.printStackTrace();
        return Result.error("对不起,操作失败,请联系管理员");
    }
    @ExceptionHandler(value = MultipartException.class)
    public Result fileUploadExceptionHandler(MultipartException exception) {
        String msg="";
        Throwable rootCase=exception.getRootCause();
        if (rootCase instanceof org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException) {
            msg="上传文件过大，文件大小不得超过1G";
        }else if(rootCase instanceof org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException){
            msg="文件上传失败，请求字节不能超过1G";
        }else {
            msg="上传失败，服务器异常";
        }
        return Result.error(msg);
    }
}
